package com.binsoft.ndtta;

import java.util.List;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Point;
import android.os.Bundle;

import com.binsoft.ndtta.R;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;

public class LocationView extends MapActivity
{
	private MapView mMapView = null;
	private MapController mMapController = null;
	@SuppressWarnings("unused")
	private static final String TAG = "LocationView";
	
    private class Marker extends Overlay
    {
    	GeoPoint mLocation = null;
    	Bitmap mMarker = null;
    	
		public Marker(GeoPoint location) 
    	{
			super();
			mLocation = location;
			mMarker = BitmapFactory.decodeResource(getResources(), R.drawable.pin);
		}

		@Override
        public void draw(Canvas canvas, MapView mapView, boolean shadow)
        {
            super.draw(canvas, mapView, shadow);

            // Draw marker
            Point screenPoint = new Point();
            mapView.getProjection().toPixels(mLocation, screenPoint);
            canvas.drawBitmap( mMarker,
                screenPoint.x - 32, screenPoint.y - 32, null);  // -32 as image is 64px high
        }
    };
    
	private List<Overlay> mMapOverlays;
    
	protected boolean isRouteDisplayed() 
	{
		return false;
	}

	private void updateMap ( Intent intent )
	{
		// Get the information
		int latitude = (int)(intent.getDoubleExtra( Constants.LATITUDE_INTENT_EXTRA, 0.0 ) * 1e+6 ) ;
		int longitude = (int)(intent.getDoubleExtra( Constants.LONGITUDE_INTENT_EXTRA, 0.0 ) * 1e+6 );
		
		GeoPoint point = new GeoPoint( latitude, longitude );
		
		mMapController.setCenter( point );
		
		mMapOverlays.clear();
		mMapOverlays.add(new Marker(point));
		
		mMapView.invalidate();
	}
	
	@Override
	protected void onCreate(Bundle icicle) 
	{
		super.onCreate(icicle);
		
		setContentView( R.layout.locationview_layout );
  
		// Set the result to CANCELED.  This will cause the widget host to cancel
        // out of the widget placement if they press the back button.
        setResult(RESULT_CANCELED);
        
        // Get the map view
        mMapView = (MapView)findViewById( R.id.realtimemapview );
        
        // Get the controller
        mMapController = mMapView.getController();
        
        // 	Setup map
        mMapView.setBuiltInZoomControls(true);
        mMapController.setZoom(15);
        
          // Get the overlays
        mMapOverlays = mMapView.getOverlays();
        
        updateMap( getIntent() );
	}

	@Override
	protected void onPause() 
	{
		super.onPause();
	}

	@Override
	protected void onResume() 
	{
		super.onResume();
	}
}
